package com.shiro.service.impl;

import com.shiro.mapper.UserMapper;
import com.shiro.mapper.UserRoleMapper;
import com.shiro.pojo.User;
import com.shiro.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public String getPassword(String name) {
        User user = getByName(name);
        if(null == user)
            return null;
        return user.getPassword();
    }

    @Override
    public User getByName(String name) {
        return userMapper.selectByName(name);
    }

    @Override
    public List<User> list() {
        return userMapper.listOrderByIdDesc();
    }

    @Override
    public void add(User user) {
        userMapper.insert(user);
    }

    @Override
    public void delete(Long id) {
        userMapper.deleteByPrimaryKey(id);
        userRoleMapper.deleteByUser(id);
    }

    @Override
    public User get(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }
}
